package com.sgu6.netty.protobuffer;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by root on 17-9-16.
 * proto包生成器
 *
 * @author sam
 */
public class PacketBuilder {

    public PacketBuilder() {
    }

    public NettyMessageProto.Header createHeader(int priority, NettyMessageProto.Header.Type type, Map<String, String> attachment) {
        NettyMessageProto.Header.Builder headerBuilder = NettyMessageProto.Header.newBuilder();
        headerBuilder.setCrcCode(1520763137);
        headerBuilder.setPriority(priority);
        headerBuilder.setType(type);
        headerBuilder.setCreateTime((new Date()).getTime());
        if (attachment == null) {
            attachment = new HashMap<String,String>();
        }

        headerBuilder.putAllAttachment(attachment);
        return headerBuilder.build();
    }

    public NettyMessageProto.Msg createMsg(String machineNo, String msg, boolean result) {
        NettyMessageProto.Msg.Builder msgBuilder = NettyMessageProto.Msg.newBuilder();
        msgBuilder.setMsg(msg);
        msgBuilder.setMachineNo(machineNo);
        msgBuilder.setResult(result);
        return msgBuilder.build();
    }

    public NettyMessageProto.Login createLogin(String machineNo, String nonceStr, long timestamp, String signature) {
        NettyMessageProto.Login.Builder loginBuilder = NettyMessageProto.Login.newBuilder();
        loginBuilder.setMachineNo(machineNo);
        loginBuilder.setNonceStr(nonceStr);
        loginBuilder.setTimestamp(timestamp);
        loginBuilder.setSignature(signature);
        return loginBuilder.build();
    }

    public NettyMessageProto.Error createError(String machineNo, int errorNo, String msg) {
        NettyMessageProto.Error.Builder errorBuilder = NettyMessageProto.Error.newBuilder();
        errorBuilder.setErrorNo(errorNo);
        errorBuilder.setMachineNo(machineNo);
        errorBuilder.setMsg(msg);
        return errorBuilder.build();
    }

    public NettyMessageProto.OrderDetail createOrderDetail(String goodsId, Integer makeNum) {
        NettyMessageProto.OrderDetail.Builder orderDetailBuilder = NettyMessageProto.OrderDetail.newBuilder();
        orderDetailBuilder.setGoodsId(goodsId);
        orderDetailBuilder.setMakeNum(makeNum);
        return orderDetailBuilder.build();
    }

    public NettyMessageProto.FinishOrder createFinishOrder(String barnId, String machineNo, String orderNo, int makeNum, int goodsNum) {
        NettyMessageProto.FinishOrder.Builder finishOrderBuilder = NettyMessageProto.FinishOrder.newBuilder();
        finishOrderBuilder.setBarnId(barnId);
        finishOrderBuilder.setMachineNo(machineNo);
        finishOrderBuilder.setOrderNo(orderNo);
        finishOrderBuilder.setMakeNum(makeNum);
        finishOrderBuilder.setGoodsNum(goodsNum);
        return finishOrderBuilder.build();
    }

    public NettyMessageProto.MachineCmd createMachineCmd(String machineNo, NettyMessageProto.MachineCmd.Cmd cmd, Integer makeNum, String riceType, String orderNo) {
        NettyMessageProto.MachineCmd.Builder machineCmdBuilder = NettyMessageProto.MachineCmd.newBuilder();
        machineCmdBuilder.setMachineNo(machineNo);
        machineCmdBuilder.setCmd(cmd);
        machineCmdBuilder.setMakeNum(makeNum);
        machineCmdBuilder.setRiceTypeOrDest(riceType);
        machineCmdBuilder.setOrderNoOrUrl(orderNo);
        return machineCmdBuilder.build();
    }

//    public NettyMessageProto.Status createStatus(String machineNo, Map<String, Integer> status) {
//        NettyMessageProto.Status.Builder statusBuilder = NettyMessageProto.Status.newBuilder();
//        statusBuilder.setHTemp(status.get("hTemp"));
//        statusBuilder.setVTemp(status.get("vTemp"));
//        statusBuilder.setWeight(status.get("weight"));
//        statusBuilder.setElectricity(status.get("electricity"));
//        statusBuilder.setMainStatus(status.get("mainStatus"));
//        statusBuilder.setDetailStatus(status.get("detailStatus"));
//        statusBuilder.setWarnValue(status.get("warnValue"));
//        statusBuilder.setOutStatus(status.get("outStatus"));
//        statusBuilder.setSwitchStatus(status.get("switchStatus"));
//        statusBuilder.setMachineNo(machineNo);
//        return statusBuilder.build();
//    }
}
